<?php

//文章标签库
class TagLibArtc extends TagLib {

	//分页栏html代码
	private $_pager;

    // 初始化标签库的定义文件
    public function _initialize() {

        $this->xml = dirname(__FILE__).'/Tags/Artc.xml';

    }


    public function _list($attr,$content)
    {
        $tag    = $this->parseXmlAttr($attr,'include');

        $pid   =  $tag['pid'];

        $size = empty($tag['size'])?10:(int)$tag['size'];

        $column = empty($tag['col'])?1:(int)$tag['col'];

        $strlen = $tag['strlen'];


	    $li = $tag['li'];

        $showDate = empty( $tag['show_date'] ) ? true:$tag['show_date'];

        $dateFormat = empty($tag['date_format']) ? 'Y-m-d':$tag['date_format'];

        $img_w = (int)$tag['img_w'];
        $img_h = (int)$tag['img_h'];

        $order = empty($tag['order'])?'id desc':$tag['order'];
        if( empty($order) )
        {
         	$order = "id desc";
        }

        $target = empty($tag['target'])?'_blank': $tag['target'];

        //是否显示分页
        $pager = $tag['pager'];
        //分页大小
        $page_size = empty($tag['page_size'])? 20:(int)$tag['page_size'];

        $dao = D('Artc');
        $dao_sort = D('Sort');

		$w = '';
		if( !empty($pid) && is_numeric($pid) )
			$pid = (int) $pid;	//pid 为指定的数字
		else if( trim($pid) == 'this' )
		{	//当前栏目
			
			if( strtolower(ACTION_NAME) == 'sort' )	//文章列表页
			{
				$pid =  (int)$_REQUEST['id'] ; //pid 为GET参数id

			}
			elseif(   strtolower(ACTION_NAME) == 'show' ) 	//文章显示页
			{
				$id = (int)$_REQUEST['id'];
				$artc = $dao->where('id='.$id)->find();
				$pid = $artc['pid']; 	//pid 为当文章的pid
				unset($artc);
			}
		}
		//判断pid是否为终极栏目
		$child_sort = $dao_sort->where('pid='.$pid)->getField('id,id');
		
		if( empty($child_sort) )	//pid为终极栏目
		{
			//设置where过滤条件
			$w = 'pid='.$pid;	
		}
		else 
		{
			//设置where过滤条件
			$w = 'pid in ('.join(',',$child_sort).')';	
		}
		
		$w = $w . ' and checked=1 and is_show=1';
		//分页
        if( $pager )
        {
         	import ( "ORG.Util.Page" );
          	$count = $dao->where( $w )->count ( 'id' ); 
         	$pg = new Page ( $count, $page_size );
            //得到分页的文章列表
         	$list = $dao->where($w)->order('top desc,`sort` asc,'.$order)->limit($pg->firstRow . ',' . $pg->listRows)->findAll();
         	$this->_pager = $pg->show();
        }
        else
        {
        	//得到$size条文章
        	$list = $dao->where($w)->order('top desc,`sort` asc,'.$order)->limit($size)->findAll();
        }

        $count = count($list);
        for($i=0;$i<$count;$i++)
        {
         	$r =  & $list[$i];
         	$id = $r['id'];

			//文章标题图片
         	$img_dir = C('ARTC_IMG_DIR');
         	if( empty($r['img']) )
         	{
           		$simg = $img = 'default.jpg';
           	}
           	else
           	{
           		$simg = 's_'.$r['img'];
           		$img = $r['img'];
           	}
         	$r['simg_url'] = $img_dir.$simg;
         	$r['img_url'] = $img_dir.$img;

          	//文章链接
          	if( empty($r['link']) )
				$r['url'] = __APP__."/Artc/show/id/".$id;
		 	else
				$r['url'] = $r['link'];

			//截取标题长度
			$r['old_title'] = $r['title'];
			if( !empty($strlen) )
	         	$r['title'] = msubstr($r['title'],0,$strlen); //substr($r['title'],0,$strlen);
            else
            	$r['title'] = $r['title'];

            //日期格式
            $r['date'] = dateFormat($r['date'],$dateFormat);



        }



		$tagTpl = (int) $tag['tpl'];   //自定义标签模板
        // 1.表格形式的列表
		if( empty( $tagTpl ) )
		{   //

	        $s = '<table width="100%"><tr>';
	        for($i=0;$i<count($list);$i++)
	        {
	        	$r = &$list[$i];

	            if( empty($img_w) || empty($img_h) )
	            {
	            	$s .= "<td>";
	            	if( !empty( $li ) )
	            	{
	            		if( is_numeric($li) )
		          			$li_img = C('LI_IMG_DIR').$li.'.gif';
		       			else
		       				$li_img = C('LI_IMG_DIR').$li;

                    	$s .= "<img src=\"{$li_img}\" align=\"absmiddle\" />&nbsp;";
                    }
					$s .= "<a href=\"{$r['url']}\" target=\"{$target}\" title=\"{$r['old_title']}\">{$r['title']}</a></td>";

		            //是否显示日期
		            if( $showDate )
		            {
		            	$date = dateFormat($r['date'],$dateFormat);
						$s .= "<td>{$date}</td>";
		            }
	            }
	            else
	            {
	            	$s .= "<td align=\"center\">";
	            	if( !empty( $li ) )
	            	{
	            		if( is_numeric($li) )
		          			$li_img = C('LI_IMG_DIR').$li.'.gif';
		       			else
		       				$li_img = C('LI_IMG_DIR').$li;

                    	$s .= "<img src=\"{$li_img}\" align=\"absmiddle\" />&nbsp;";
                    }
	            	$s .= "<a href=\"{$r['url']}\" target=\"{$target}\"><img src=\"{$r['simg_url']}\" width=\"$img_w\" height=\"{$img_h}\" border=\"0\" style=\"margin-bottom:4px;\" title=\"{$r['old_title']}\"><br />";
	            	$s .= $r['title'];
	            	$s .= "</a></td>";
	            }

	            if(  (($i+1) % $column )   == 0 )
		        	$s .= '</tr><tr>';

	        }
	        $s .= '</tr></table>';

        	return $s;
        }
        else	//2. 可自定义标签模板的调用
        {
         	$file = C('TPL_PATH').$tagTpl.'.html';
            $view = Think::instance('View');


			//得到文章的栏目信息
			$sort = $dao_sort->find($pid);
		   	$sort['title']  = $sort['name'];
		   	//栏目标题图片
         	$img_dir = C('ARTC_IMG_DIR');
         	if( empty($r['img']) )
         	{
           		$simg = $img = 'default.jpg';
           	}
           	else
           	{
           		$simg = 's_'.$r['img'];
           		$img = $r['img'];
           	}
         	$sort['simg_url'] = $img_dir.$simg;
         	$sort['img_url'] = $img_dir.$img;
         	//栏目链接
            //栏目链接
          	if( empty($r['link']) )
				$sort['url'] = __APP__."/Artc/sort/id/".$id;
		 	else
				$sort['url'] = $r['link'];


			$view->assign('list',$list);
			$view->assign('img_w',$img_w);
			$view->assign('img_h',$img_h);
			$view->assign('target',$target);
			$view->assign('strlen',$strlen);
			if( $pager  )	//输出分页栏
				$view->assign('pager',$this->_pager);
			$view->assign('sort',$sort);

          	return $view->fetch($file);

        }


    }


	/**
	   *  分页栏标签
	   *
	   */
    function _pager($attr,$content)
    {
     	return $this->_pager;
    }


    /**
       *   显示文章的字段信息
       *
       */
    function _show($attr,$content)
    {
        $tag    = $this->parseXmlAttr($attr,'include');

		$id = (int) $tag['id'];

		$field = empty($tag['field'])?'content':$tag['field'];

		$img_w = (int)$tag['img_w'];
		$img_h = (int)$tag['img_h'];

		$show_title = empty($tag['show_title'])?'':$tag['show_title'];

		$strlen = (int)$tag['strlen'];
		if( empty($strlen) )
			$strlen=50;

		$target = $tag['target'];

		$dao = D('Artc');

		$vo = $dao->where('id='.$id)->find();

		if( empty($vo) )
			return 'ID为：'.$id.'的文章不存在！';

		//文章标题图片
       	$img_dir = C('ARTC_IMG_DIR');
       	if( empty($vo['img']) )
       	{
        	$simg = $img = 'default.jpg';
        }
        else
        {
        	$simg = 's_'.$vo['img'];
        	$img = $vo['img'];
        }
        $vo['simg_url'] = $img_dir.$simg; //文章图片(缩略图)
       	$vo['img_url'] = $img_dir.$img; //文章图片

        $vo['cut_content'] = msubstr(strip_tags($vo['content']),0,$strlen);	//截取字符串后的文章内容

        //文章链接
		if( empty($r['link']) )
			$vo['url'] = __APP__."/Artc/show/id/".$id;
		else
			$vo['url'] = $vo['link'];

        $tagTpl = (int) $tag['tpl'];   //自定义标签模板
        // 1.表格形式的列表
		if( empty( $tagTpl ) )
		{
         	$s = '<table width="100%"><tr>';
         	$s .= '<td align="left" style="line-height:18px;">';
         	$s .= "<a href=\"{$vo['url']}\" target=\"{$target}\">";
         	if( !empty($img_w) && !empty($img_h) )	//显示图片
         	{
         		$s .= "<img src=\"{$vo['simg_url']}\" width=\"$img_w\" height=\"{$img_h}\" border=\"0\" align=\"left\" style=\"margin-bottom:4px;margin-right:4px;\" title=\"{$vo['title']}\">";
         		if( !empty( $show_title ) )
         			$s .= '<br />'.$vo['title'];
         	}

			$s .= $vo['cut_content'].'...';


         	$s .= '</a></td></tr></table>';

         	return $s;

		}
		else  //2. 可自定义标签模板的调用
		{
       		$file = C('TPL_PATH').$tagTpl.'.html';
            $view = Think::instance('View');

			$view->assign('list',$list);
			$view->assign('img_w',$img_w);
			$view->assign('img_h',$img_h);
			$view->assign('target',$target);
			$view->assign('show_title',$show_title);

			return $view->fetch($file);

		}

    }


    /**
       *  栏目列表标签
       *
       */
   function _sort_list($attr,$content)
   {

        $tag    = $this->parseXmlAttr($attr,'include');

        $pid   = $tag['pid'];

        $size = empty($tag['size'])?10:(int)$tag['size'];

        $column = empty($tag['col'])?1:(int)$tag['col'];

        $strlen = $tag['strlen'];


	    $li = $tag['li'];



        $img_w = (int)$tag['img_w'];
        $img_h = (int)$tag['img_h'];

        $order = empty($tag['order'])?'id desc':$tag['order'];
        if( empty($order) )
        {
         	$order = "id desc";
        }

        $target = empty($tag['target'])?'_self': $tag['target'];

        $dao = D('Sort');

		$w = '';

		if( trim($pid)=='' || is_numeric($pid) )
			$pid = (int)$pid;
		else if( trim($pid) == 'this' )
		{	//当前栏目

			if( strtolower(ACTION_NAME) == 'sort' )	//文章列表页
			{
				$id =  (int)$_REQUEST['id'] ; //pid 为GET参数id
				$sort = $dao->where('pid='.$id)->find();
				if( !empty($sort) )
					$pid = $id;
				else
				{
					$sort = $dao->where('id='.$id)->find();
					$pid = $sort['pid'];
					unset($sort);

				}



			}
			elseif(   strtolower(ACTION_NAME) == 'show' ) 	//文章显示页
			{
				$dao_artc = D('Artc');
				$artc = $dao_artc->find( (int)$_REQUEST['id'] );
				$id = $artc['pid']; 	//pid 为当文章的pid
				$sort = $dao->where('id='.$id)->find();
				$pid = $sort['pid'];
				unset($artc);
			}


		}
		$w = 'pid='.$pid;


        $list = $dao->where($w)->order('`sort` asc,'.$order)->limit($size)->findAll();

        $sum = count($list);
        for($i=0;$i<$sum;$i++)
        {
         	$r =  & $list[$i];
         	$id = $r['id'];

			//栏目标题图片
         	$img_dir = C('ARTC_IMG_DIR');
         	if( empty($r['img']) )
         	{
           		$simg = $img = 'default.jpg';
           	}
           	else
           	{
           		$simg = 's_'.$r['img'];
           		$img = $r['img'];
           	}
         	$r['simg_url'] = $img_dir.$simg;
         	$r['img_url'] = $img_dir.$img;


          	//栏目链接
          	if( empty($r['link']) )
				$r['url'] = __APP__."/Artc/sort/id/".$id;
		 	else
				$r['url'] = $r['link'];

			//截取标题长度
			$r['old_title'] = $r['name'];
			if( !empty($strlen) )
	         	$r['title'] = msubstr($r['name'],0,$strlen); //substr($r['title'],0,$strlen);
            else
            	$r['title'] = $r['name'];



        }

        //自定义标签模板
		$tagTpl = (int) $tag['tpl'];
		if( empty( $tagTpl ) )
		{   //表格形式的列表

	        $s = '<table width="100%"><tr>';
	        for($i=0;$i<count($list);$i++)
	        {
	        	$r = &$list[$i];

	            if( empty($img_w) || empty($img_h) )
	            {
	            	$s .= "<td>";
	            	if( !empty( $li ) )
	            	{
	            		if( is_numeric($li) )
		          			$li_img = C('LI_IMG_DIR').$li.'.gif';
		       			else
		       				$li_img = C('LI_IMG_DIR').$li;

                    	$s .= "<img src=\"{$li_img}\" align=\"absmiddle\" />&nbsp;";
                    }
					$s .= "<a href=\"{$r['url']}\" target=\"{$target}\" title=\"{$r['old_title']}\">{$r['title']}</a></td>";

		            //是否显示日期
		            if( $showDate )
		            {
		            	$date = dateFormat($r['date'],$dateFormat);
						$s .= "<td>{$date}</td>";
		            }
	            }
	            else
	            {
	            	$s .= "<td align=\"center\">";
	            	if( !empty( $li ) )
	            	{
	            		if( is_numeric($li) )
		          			$li_img = C('LI_IMG_DIR').$li.'.gif';
		       			else
		       				$li_img = C('LI_IMG_DIR').$li;

                    	$s .= "<img src=\"{$li_img}\" align=\"absmiddle\" />&nbsp;";
                    }
	            	$s .= "<a href=\"{$r['url']}\" target=\"{$target}\"><img src=\"{$r['simg_url']}\" width=\"$img_w\" height=\"{$img_h}\" border=\"0\" style=\"margin-bottom:4px;\" title=\"{$r['old_title']}\"><br />";
	            	$s .= $r['title'];
	            	$s .= "</a></td>";
	            }

	            if(  (($i+1) % $column )   == 0 )
		        	$s .= '</tr><tr>';

	        }
	        $s .= '</tr></table>';

        	return $s;
        }
        else	//可自定义标签模板的调用
        {
         	$file = C('TPL_PATH').$tagTpl.'.html';
            $view = Think::instance('View');

			$view->assign('list',$list);
			$view->assign('img_w',$img_w);
			$view->assign('img_h',$img_h);
			$view->assign('target',$target);
          	return $view->fetch($file);

        }

   }








}




?>